﻿using BoardGame;
using GameModelView;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;


namespace WpfChess
{
    /// <summary>Main.</summary>
    public partial class App : Application
    {
    }


    public class ChessWpfView
    {
        private ChessWpfController wpfController;
        private ChessModelView chessModelView;

        internal ChessWpfView (ChessWpfController newWpfController, ChessModelView newChessModelView)
        {
            this.wpfController = newWpfController;
            SetChessView (newChessModelView);
        }


        public void SetChessView (ChessModelView newChessModelView)
        {
            this.chessModelView = newChessModelView;

            newChessModelView.GameCreated += ShowNewGame;
            newChessModelView.StatusUpdate += UpdateStatus;
            newChessModelView.TokenRemove += ClearToken;
            newChessModelView.TokenPlace += PlaceToken;
            newChessModelView.MoveProcess += ProcessMove;
            newChessModelView.UndoPreview += PreviewUndo;
            newChessModelView.HiliteChange += wpfController.ChangeHilite;
            newChessModelView.HilitesClear += wpfController.ClearHilites;

            ShowNewGame ();
            UpdateStatus ();
        }


        private void ShowNewGame ()
        {
            wpfController.moveList.Items.Clear ();

            for (int y = 0; y < chessModelView.Board.YSize; ++y)
                for (int x = 0; x < chessModelView.Board.XSize; ++x)
                {
                    Piece piece = chessModelView.Board[x, y] as Piece;
                    if (piece != null)
                        PlaceToken (x, y, piece.Side, piece.Token);
                    else
                        ClearToken (x, y);
                }
        }


        private void UpdateStatus ()
        {
            wpfController.promoList.Text = chessModelView.CurrentPlayerPromotionName;
            string[] list = chessModelView.GetStatus ();

            wpfController.statusTurn.Text = list[0];
            wpfController.status1.Text = list[1];
            wpfController.status2.Text = list[2];

            UpdateEnables ();
        }


        private void UpdateEnables ()
        {
            bool isPlaying = chessModelView != null && chessModelView.IsUnderway;

            wpfController.gameMenuResign.IsEnabled = isPlaying;
            wpfController.gameMenuUndo.IsEnabled = isPlaying && chessModelView.Plies.Count > 0;
            wpfController.promoList.IsEnabled = isPlaying;
            wpfController.drawMenu.IsEnabled = isPlaying;
            wpfController.drawMenuNoCaptures.IsEnabled = isPlaying && chessModelView.Is50MoveRuleClaimable;
            wpfController.drawMenuSame6.IsEnabled = isPlaying && chessModelView.Same6Moves;
            wpfController.drawMenuRequest.IsEnabled = isPlaying;
        }


        private void ClearToken (int x, int y)
        {
            TextBlock cell = (TextBlock) wpfController.boardGrid.Children[(7 - y) * 8 + x];
            cell.Text = "";
        }


        private void PlaceToken (int x, int y, Player player, Token token)
        {
            string[] p0 = { "-", "King", "Queen", "Rook", "Bishop", "Knight", "Pawn" };

            TextBlock cell = (TextBlock) wpfController.boardGrid.Children[(7 - y) * 8 + x];

            if (token != Token.Empty)
            {
                cell.Foreground = player == Player.White? Brushes.White : Brushes.Black;
                cell.Text = p0[(int) token];
            }
        }


        private void ProcessMove ()
        {
            int lastItem;
            Ply ply = chessModelView.Plies.Top;
            string newText = ply.ToString ();

            wpfController.ClearSelection ();

            if (ply.Moving.Side == Player.Black)
            {
                lastItem = wpfController.moveList.Items.Count - 1;
                string text = (string) wpfController.moveList.Items[lastItem];
                wpfController.moveList.Items[lastItem] = text + "  " + newText;
            }
            else
            {
                int turnNumber = chessModelView.Plies.Count / 2 + 1;
                lastItem = wpfController.moveList.Items.Count;
                wpfController.moveList.Items.Add (turnNumber.ToString () + ".  " + newText);
            }

            wpfController.moveList.Focus ();
            wpfController.moveList.SelectedItem = wpfController.moveList.Items[lastItem];
        }


        private void PreviewUndo ()
        {
            int lastItem = (chessModelView.Plies.Count - 1) / 2;
            if (chessModelView.Plies.Count % 2 == 0)
                wpfController.moveList.Items[lastItem] = (lastItem + 1).ToString () + ".  " + chessModelView.Plies[chessModelView.Plies.Count - 2].ToString ();
            else
                wpfController.moveList.Items.RemoveAt (lastItem);
        }

    }
}
